package com.etc.delivery.controller.admin;

import com.etc.delivery.dto.WaybillDto;
import com.etc.delivery.entity.MailType;
import com.etc.delivery.entity.Waybill;
import com.etc.delivery.service.admin.IAdminWaybillService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/admin")
@Controller
public class AdminWaybillController {

    @Resource
    private IAdminWaybillService waybillService;

    @RequestMapping("/queryWaybill")
    @ResponseBody
    public PageInfo<WaybillDto> queryWaybill(String waybillNo, Integer mailType, Integer warehouseId, Integer pageNum, Integer pageSize) {

        if (mailType == null) {
            mailType = 0;
        }

        if (warehouseId == null) {
            warehouseId = 0;
        }

        return waybillService.queryWaybill(waybillNo, mailType, warehouseId, pageNum, pageSize);
    }

    @RequestMapping(value = "deleteWaybill")
    @ResponseBody
    public Map<String, String> deleteWaybill(String waybillNo) {
        Map<String, String> map = new HashMap<>();
        boolean flag = waybillService.deleteWaybill(waybillNo);
        if (flag) {
            map.put("code", "1");
            map.put("msg", "删除成功");
        } else {
            map.put("code", "0");
            map.put("msg", "删除失败");
        }
        return map;
    }

    @RequestMapping(value = "updateWaybill")
    @ResponseBody
    public Map<String, String> updateWaybill(String waybillNo, Integer mailType, String senderName, String senderPhone, String senderAddress, String recipientName, String recipientPhone, String recipientAddress) {
        Waybill waybill = new Waybill();
        waybill.setWaybillNo(waybillNo);
        waybill.setSenderName(senderName);
        waybill.setSenderPhone(senderPhone);
        waybill.setSenderAddress(senderAddress);
        waybill.setMailType(mailType);
        waybill.setRecipientName(recipientName);
        waybill.setRecipientPhone(recipientPhone);
        waybill.setRecipientAddress(recipientAddress);
//        waybill.setStatus(status);

        boolean flag = waybillService.updateWaybill(waybill);

        Map<String, String> map = new HashMap<>();
        if (flag) {
            map.put("code", "1");
            map.put("msg", "修改成功");
        } else {
            map.put("code", "0");
            map.put("msg", "修改失败");
        }
        return map;

    }

    @RequestMapping(value = "queryMailName")
    @ResponseBody
    public List<MailType> queryMailName() {
        return waybillService.queryMailName();
    }

}
